package com.glsc.ngateway.opmanage.service.archive;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.archive.ArchiveClient;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.archive.CounterParty;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.archive.ArchiveClientRepository;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.archive.CounterPartyRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class CounterPartyService {

    @Resource
    private CounterPartyRepository counterPartyRepository;
    @Resource
    private ArchiveClientRepository archiveClientRepository;

    /**
     * 保存交易对手方信息
     *
     * @param counterParty
     * @return
     */
    public CounterParty save(CounterParty counterParty) {
        if (Objects.isNull(counterParty)) {
            return null;
        }

        List<CounterParty> list = counterPartyRepository.findAll(Example.of(CounterParty.builder().partyName(counterParty.getPartyName()).build()));
        List<CounterParty> dataList = Lists.newArrayList();
        if (StringUtils.isNotBlank(counterParty.getPartyOrganCode())) {
            dataList = counterPartyRepository.findAll(Example.of(CounterParty.builder().partyOrganCode(counterParty.getPartyOrganCode()).build()));
        }

        //新增要求交易对手名字和组代都不允许重复
        if (Objects.isNull(counterParty.getId())) {
            Assert.isTrue(CollectionUtils.isEmpty(list), "名称为[" + counterParty.getPartyName() + "]的交易对手方已存在");
            if (StringUtils.isNotBlank(counterParty.getPartyOrganCode())) {
                Assert.isTrue(CollectionUtils.isEmpty(dataList), "组织机构代码为[" + counterParty.getPartyOrganCode() + "]的交易对手方已存在");
            }
        } else {
            //修改时也不能跟别人重复
            if (CollectionUtils.isNotEmpty(list)) {
                if (list.get(0).getId() != counterParty.getId()) {
                    Assert.isTrue(false, "名称为[" + counterParty.getPartyName() + "]的交易对手方已存在");
                }
            }

            if (StringUtils.isNotBlank(counterParty.getPartyOrganCode())) {
                if (CollectionUtils.isNotEmpty(dataList)) {
                    if (dataList.get(0).getId() != counterParty.getId()) {
                        Assert.isTrue(false, "组织机构代码为[" + counterParty.getPartyOrganCode() + "]的交易对手方已存在");
                    }
                }
            }
        }

        return counterPartyRepository.save(counterParty);
    }

    /**
     * 根据id查找交易对手方信息
     *
     * @param id
     * @return
     */
    public CounterParty findById(Integer id) {
        if (Objects.isNull(id)) {
            return null;
        }
        Optional<CounterParty> optional = counterPartyRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 分页查找交易对手方信息
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<CounterParty> findCounterPartyPage(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<CounterParty> page = counterPartyRepository.findAll(SpecificationUtil.buildSpecification(param, CounterParty.class), pageable);

        return page;
    }

    /**
     * 条件查询交易对手方信息
     *
     * @param param
     * @return
     */
    public List<CounterParty> listByCondition(Map<String, Object> param) {
        if (Objects.isNull(param)) {
            return null;
        }
        List<CounterParty> list = counterPartyRepository.findAll(SpecificationUtil.buildSpecification(param, CounterParty.class));
        return list;
    }

    /**
     * 条件查找
     *
     * @param counterParty
     * @return
     */
    public List<CounterParty> findByCondition(CounterParty counterParty) {
        if (Objects.isNull(counterParty)) {
            return null;
        }
        List<CounterParty> list = counterPartyRepository.findAll(Example.of(counterParty));
        return list;
    }


    /**
     * 根据id禁用交易对手方
     *
     * @param id
     */
    public void delete(Integer id) {
        if (Objects.isNull(id)) {
            return;
        }
        long count = archiveClientRepository.count(Example.of(ArchiveClient.builder().partyId(id).build()));
        Assert.isTrue(count <= 0, "交易对手在使用中，禁止删除");
        counterPartyRepository.deleteById(id);
    }

    /**
     * 查询所有在使用状态的交易对手方信息
     *
     * @return
     */
    public List<CounterParty> listAllEnablePart() {
        List<CounterParty> list = counterPartyRepository.findAll(Example.of(CounterParty.builder().enableFlag("Y").build()));
        return list;
    }


}
